package net.csdra.district.component;

import net.csdra.basis.Utils;
import net.csdra.basis.service.CurrentUserInfo;
import net.csdra.basis.service.ServiceBaseInterfaceImpl;
import net.csdra.district.domain.District;
import net.csdra.district.domain.DistrictRepository;
import net.csdra.district.component.form.CreateDistrict;
import net.csdra.district.component.form.UpdateDistrict;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by msf on 2018/1/25.
 */
public class DistrictComponentImpl extends ServiceBaseInterfaceImpl<District, CreateDistrict, UpdateDistrict> implements DistrictComponent {

    DistrictRepository jpaRepository;

    public DistrictComponentImpl(DistrictRepository jpaRepository, CurrentUserInfo currentUserInfo) {
        super(District.class, jpaRepository, currentUserInfo);
        this.jpaRepository = jpaRepository;
    }

//    @Override
//    public District create(CreateDistrict createDistrict) {
//        Assert.notNull(createDistrict, "参数不能为空");
//        Assert.hasLength(createDistrict.getName(), "name的值不能为空");
//        District district = super.create(createDistrict);
//        district.setName(createDistrict.getName());
//        district.setInfo(createDistrict.getInfo());
//        jpaRepository.save(district);
//        District result = new District();
//        Utils.copy(district, result);
//        return result;
//    }

//    @Override
//    public District update(UpdateDistrict updateDistrict) {
//        Assert.notNull(updateDistrict, "参数不能为空");
//        Assert.hasLength(updateDistrict.getName(), "name的值不能为空");
//        District update = super.update(updateDistrict);
//        Utils.copy(update, update);
//        jpaRepository.save(update);
//        District result = new District();
//        Utils.copyProperties(update, result);
//        return result;
//    }

    @Override
    public List<District> getPath(String currentDistrictId) {
        Assert.hasLength(currentDistrictId, "currentDistrictId不能为空");
        List<District> result = new ArrayList<>();
        String metaId = currentDistrictId;
        while (!"ROOT".equals(metaId)) {
            District district = jpaRepository.findOne(currentDistrictId);
            result.add(district);
            metaId = district.getMetaId();
        }
        return result;
    }
}
